/**
 * path 路径转换成node节点
 * @class PathD2Node
 * @author iwangcj
 */
export default class PathD2Node {
    constructor() {

    }

    static convert(d) {
        const nodeList = [];

        if (d && typeof d === 'string') {

            let formindex = 0;
            const cmdposlist = []; //每个命令的索引位置
            const cmddatalist = []; //每个命令后面的数据
            const cmdlist = d.match(/([a-z]|[A-Z])/g); //命令列表
            //console.log(cmdlist);
            for (let i = 0; i < cmdlist.length; i++) {
                const index = d.indexOf(cmdlist[i], formindex);
                // console.log(cmdlist[i],index);
                cmdposlist.push(index);
                if (i > 0) {
                    const data = d.substring(formindex, index);
                    cmddatalist.push(data);
                }
                formindex = index + 1;

                // console.log("----------------------------------")
            }
            if (cmdposlist.length > 0) {
                const data = d.substring(cmdposlist[cmdposlist.length - 1] + 1);
                cmddatalist.push(data)
            }

            const isComma = d.split(",").length;
            for (let i = 0; i < cmdlist.length; i++) {
                const cmd = cmdlist[i];
                let data = cmddatalist[i];
                if (isComma) {
                    // console.log(data);
                    data = data.replace(/(^\s*)|(\s*$)/g, "");
                    const spaceData = data.split(" ");
                    // console.log(cmd, data, spaceData);
                    let node = {
                        cmd: cmd
                    }
                    let CMD = cmd.toUpperCase();
                    if (CMD == "C") {
                        const c1 = spaceData[0] ? spaceData[0].split(",") : spaceData[0];
                        const c2 = spaceData[1] ? spaceData[1].split(",") : spaceData[1];
                        const pos = spaceData[2] ? spaceData[2].split(",") : spaceData[2];
                        node = {
                            x: pos[0],
                            y: pos[1],
                            cx1: c1[0],
                            cy1: c1[1],
                            cx2: c2[0],
                            cy2: c2[1],
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD == "A") {
                        const r = spaceData[0] ? spaceData[0].split(",") : spaceData[0];
                        const rotation = spaceData[1];
                        const arc = spaceData[2];
                        const sweep = spaceData[3];
                        const pos = spaceData[4] ? spaceData[4].split(",") : spaceData[4];
                        node = {
                            x: pos[0],
                            y: pos[1],
                            rx: r[0],
                            ry: r[1],
                            rotation,
                            arc,
                            sweep,
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD === 'Q') {
                        const c = spaceData[0] ? spaceData[0].split(",") : spaceData[0];
                        const pos = spaceData[1] ? spaceData[1].split(",") : spaceData[1];
                        node = {
                            x: pos[0],
                            y: pos[1],
                            cx: c[0],
                            cy: c[1],
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD === 'S') {
                        const c2 = spaceData[0] ? spaceData[0].split(",") : spaceData[0];
                        const pos = spaceData[1] ? spaceData[1].split(",") : spaceData[1];
                        node = {
                            x: pos[0],
                            y: pos[1],
                            cx2: c2[0],
                            cy2: c2[1],
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD === 'M' || CMD === 'L' || CMD === 'T') {
                        const pos = spaceData[0] ? spaceData[0].split(",") : spaceData[0];
                        node = {
                            x: pos[0],
                            y: pos[1],
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD === 'H') {
                        node = {
                            x: data,
                            cmd,
                        }
                        nodeList.push(node);
                    } else if (CMD === 'V') {
                        node = {
                            y: data,
                            cmd,
                        }
                        nodeList.push(node);
                    }
                }
            }
        }

        return nodeList;
    }

}